<?php

//图片验证码中间件

namespace app\api\middleware;
use support\exception\ValidateException;
use Webman\MiddlewareInterface;
use Webman\Http\Response;
use Webman\Http\Request;
use Lengyue\Captcha\Captcha;


class CaptchaAuth implements MiddlewareInterface
{
	
    public function process(Request $request, callable $handler) : Response
	{
		$captcha	= $request->post('captcha');	//验证码
		$key		= $request->post('key');	//验证码id
			
		if(empty($captcha)){
			return json(['status'=>411,'msg'=>'验证码不能为空']);
		}
		
		if(empty($key)){
			return json(['status'=>411,'msg'=>'验证码key不能为空']);
		}
		
		if(false === Captcha::check($captcha,$key)){
			return json(['status'=>411,'msg'=>'验证码错误']);
		}
		
		return $handler($request);
    }
    
} 